package com.memo4android;

import com.memo4android.model.AppModel;
import com.memo4android.model.Dossier;
import com.memo4android.model.ItemNote;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class NoteActivity extends Activity implements OnClickListener{
	
	private ItemNote note;
	private Dossier dossier;
	private Spinner spinner;
	private ArrayAdapter<CharSequence> m_adapterForSpinner;
	private AppModel model;
	private int positionDefault=-1;
	private String folderName;
	
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_postit_note);
        
        model = AppModel.getInstance(this);
        folderName = getIntent().getStringExtra("folderName");
        String noteName = getIntent().getStringExtra("noteName");
        setTitle(this.getTitle() + " - " + folderName + "/" + noteName);
        dossier = model.getDossier(folderName);
        note = dossier.getNote(noteName);
        if(note != null){ 
        	TextView title = (TextView) findViewById(R.id.titre);
        	TextView content = (TextView) findViewById(R.id.corps);
        	title.setText(note.getTitre());
        	content.setText(note.getContent());
        }
        
        Button btValid=(Button)findViewById(R.id.button_validEditingNote);
        btValid.setOnClickListener(this);
        
        Button btCancel=(Button)findViewById(R.id.button_CancelEditingNote);
        btCancel.setOnClickListener(this);
        
        Button btDelete=(Button)findViewById(R.id.button_DeleteEditingNote);
        btDelete.setOnClickListener(this);
        
        
        spinner=(Spinner) findViewById(R.id.spinnerEditingNote);
        
        m_adapterForSpinner = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
        m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(m_adapterForSpinner);
        
        int i;
        for(i=0;i<model.getNumberOfDossiers();i++){
        	m_adapterForSpinner.add(model.getDossier(i).getTitre().toString());
        	if(model.getDossier(i).equals(dossier)){
        		positionDefault=i;
        	}
		}
        
        m_adapterForSpinner.add(model.getHome().getTitre().toString());
        
        //selection du spinner par default home sinon le dossier en question
        if(positionDefault==-1){
        	positionDefault=i+1;
        }
        
        spinner.setSelection(positionDefault);
        
    }
    
    @Override
    public void onBackPressed(){
    	if(folderName.equals("__HOME")){
			Intent intent = new Intent(this ,MainView.class);
			this.startActivity(intent);  
    	} else {
			Intent intent = new Intent(this ,InFolderActivity.class);
			intent.putExtra("folderName", folderName);
			this.startActivity(intent);      		
    	}
    }    

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.button_validEditingNote:
			EditText contentEdit=(EditText) findViewById(R.id.corps);
			String content=contentEdit.getText().toString();
			EditText titleEdit=(EditText) findViewById(R.id.titre);
			String title=titleEdit.getText().toString();
			if(title.equals("")||content.equals("")){
				//completer avec le message box erreur
			}
			else{
				String folderSelected=spinner.getSelectedItem().toString();
				Dossier dossierNew=model.getDossier(folderSelected);
				
				if(dossier.equals(dossierNew)){
					note.setContent(content);
					note.setTitre(title);
					//enregistrement des modifications (bdd)
					model.saveNoteToBDD(note);
				}
				else{
					dossier.deleteNote(note);
					//enregistrement de la note dans son dossier (model et bdd)
					model.addNote(dossierNew, new ItemNote(dossierNew,title,content));
				}
				Intent intentDossier;
				if(dossierNew.isHome())
					intentDossier = new Intent(this, MainView.class);
				else{
					intentDossier = new Intent(this, InFolderActivity.class);
					intentDossier.putExtra("folderName", dossierNew.getTitre());
				}	
				this.finish();
			}
			
			
			break;
		case R.id.button_CancelEditingNote:
			this.finish();
			break;
		case R.id.button_DeleteEditingNote:
			dossier.deleteNote(note);
			model.deleteNoteFromBDD(note);
			Intent intentDossier;
			if(dossier.isHome())
				intentDossier = new Intent(this, MainView.class);
			else{
				intentDossier = new Intent(this, InFolderActivity.class);
				intentDossier.putExtra("folderName", dossier.getTitre());
			}	
			this.startActivity(intentDossier);			
			this.finish();
			break;
		
		}
		
	}
}